package com.sunshine.domain.service.impl;

import com.sunshine.domain.convert.SubjectCategroyConvert;
import com.sunshine.domain.convert.SubjectLabelConvert;
import com.sunshine.domain.entity.SubjectCategroyBO;
import com.sunshine.domain.entity.SubjectLabelBO;
import com.sunshine.domain.service.SubjectLabelDomainService;
import com.sunshine.infra.basic.entity.SubjectLabel;
import com.sunshine.infra.basic.entity.SubjectMapping;
import com.sunshine.infra.basic.service.SubjectLabelService;
import com.sunshine.infra.basic.service.SubjectMappingService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import javax.annotation.Resource;
import java.util.Collections;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import java.util.function.Function;
import java.util.stream.Collectors;

/**
 * 描述: 题目标签服务实现类
 *
 * @version 1.0
 * @Author 夏百友
 * @Date 2025/11/5 18:19
 */
@Service
@Slf4j
public class SubjectLabelDomainServiceImpl implements SubjectLabelDomainService {

    @Resource
    private SubjectLabelService subjectLabelService;

    @Resource
    private SubjectMappingService subjectMappingService;

    /**
     * 添加题目标签
     *
     * @author 夏百友
     * @date 2025/11/5 18:31
     */

    @Override
    public Boolean add(SubjectLabelBO subjectLabelBO) {

        if (log.isInfoEnabled()) {
            log.info("domain.service.SubjectLabelDomainServiceImpl.add:参数{}", subjectLabelBO);
        }

        SubjectLabel insert = subjectLabelService.insert(SubjectLabelConvert.INSTANCE.convertBOToLabel(subjectLabelBO));
        log.info("domain.service.SubjectLabelDomainServiceImpl.add:结果{}", insert);
        return insert != null;
    }

    @Override
    public Boolean update(SubjectLabelBO subjectLabelBO) {
        if (log.isInfoEnabled()) {
            log.info("domain.service.SubjectLabelDomainServiceImpl.update:参数{}", subjectLabelBO);
        }
        SubjectLabel update = subjectLabelService.update(SubjectLabelConvert.INSTANCE.convertBOToLabel(subjectLabelBO));
        log.info("domain.service.SubjectLabelDomainServiceImpl.update:结果{}", update);
        return update != null;
    }

    @Override
    public Boolean delete(SubjectLabelBO subjectLabelBO) {
        if (log.isInfoEnabled()) {
            log.info("domain.service.SubjectLabelDomainServiceImpl.delete:参数{}", subjectLabelBO);
        }
        Boolean delete = subjectLabelService.deleteById(subjectLabelBO.getId());
        log.info("domain.service.SubjectLabelDomainServiceImpl.delete:结果{}", delete);
        return delete;
    }

    @Override
    public List<SubjectLabelBO> queryLabelByCategroyId(SubjectLabelBO subjectLabelBO) {
        if (log.isInfoEnabled()) {
            log.info("domain.service.SubjectLabelDomainServiceImpl.queryLabelByCategroyId:参数{}", subjectLabelBO);
        }
        Long categoryId = subjectLabelBO.getCategoryId();
        SubjectMapping subjectMapping = new SubjectMapping();
        subjectMapping.setCategoryId(categoryId);
        List<SubjectMapping> subjectMappings = subjectMappingService.queryList(subjectMapping);

        if (CollectionUtils.isEmpty(subjectMappings)) {
            return Collections.emptyList();
        }
        List<Long> collect = subjectMappings.stream().map(SubjectMapping::getLabelId).collect(Collectors.toList());

        List<SubjectLabel> subjectLabels = subjectLabelService.batchQuerById(collect);

        List<SubjectLabelBO> subjectLabelBOS = new LinkedList<>();

        // 创建一个Map来关联labelId和SubjectLabel对象
        Map<Integer, SubjectLabel> labelMap = subjectLabels.stream()
                .collect(Collectors.toMap(SubjectLabel::getId, Function.identity()));

        subjectMappings.forEach(mapping -> {
            Long labelId = mapping.getLabelId();
            SubjectLabel label = labelMap.get(labelId.intValue());
            // 只有当标签存在时才添加到结果中
            if (label != null) {
                SubjectLabelBO BO = new SubjectLabelBO();
                BO.setId(label.getId());
                BO.setLabelName(label.getLabelName());
                BO.setLabelId(labelId);
                BO.setCategoryId(categoryId);
                BO.setSortNum(label.getSortNum());
                subjectLabelBOS.add(BO);
            }
        });
        log.info("domain.service.SubjectLabelDomainServiceImpl.queryLabelByCategroyId:结果{}", subjectLabelBOS);
        return subjectLabelBOS;

    }
}
